
#Author: Luke Keele
#Matching Analysis for Black Mayoral Turnout in LA
#Date: 6/5/2013

#Load Libraries
library(foreign)
library(rbounds)
library(mipmatch)
library(xtable)

rm(list=ls())

setwd("~/Documents/Black Mayors/Replication File/Match Functions")

source("errorhandling.R")
source("constraintmatrix2.R")
source("problemparameters2.R")
source("subsetmatch2.R")

###################################################################
# Match One - General Elections
###################################################################
setwd("~/Documents/Black Mayors/Replication File/Data")

vturn <- read.dta("match-all.dta")
vturn <- vturn[,c("black", "pop90", "blackpop_pct1990", "year", 
				   "black_turnout", "fipscode", "city_name", "year", "day", 
				   "month", "no_cand", "college_pct", "unemp", "income", 
				   "poverty", "hs_pct", "home") ]
vturn = vturn[order(vturn$black, decreasing = TRUE), ]
head(vturn)
tail(vturn)
n <- nrow(vturn)
vturn$id <- seq(1:n)

# Treatment indicator 
	t_ind = vturn$black

# Matrix of covariates
	X_mat = cbind(vturn$pop90, vturn$blackpop_pct1990, vturn$college_pct, 
				  vturn$hs_pct, vturn$unemp, vturn$income, vturn$poverty, vturn$home)

#Prematch Balance		  	  
KS_out_pre <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home, 
							   ks=TRUE, nboots=1000, data=vturn)

out_pre <- matrix(NA, 8,3)

for (i in 1:8){
	out_pre[i,1] <- unlist((KS_out_pre$BeforeMatching)[[i]])[3]
  	out_pre[i,2] <- unlist((KS_out_pre$BeforeMatching)[[i]])[4]
  	out_pre[i,3] <- unlist((KS_out_pre$BeforeMatching)[[i]])[11]

}
out_pre

# Convert Proportions to Percentages
out_pre[2,1] <-  out_pre[2,1]*100
out_pre[3,1] <-  out_pre[3,1]*100
out_pre[4,1] <-  out_pre[4,1]*100
out_pre[5,1] <-  out_pre[5,1]*100
out_pre[7,1] <-  out_pre[7,1]*100
out_pre[8,1] <-  out_pre[8,1]*100

out_pre[2,2] <-  out_pre[2,2]*100
out_pre[3,2] <-  out_pre[3,2]*100
out_pre[4,2] <-  out_pre[4,2]*100
out_pre[5,2] <-  out_pre[5,2]*100
out_pre[7,2] <-  out_pre[7,2]*100
out_pre[8,2] <-  out_pre[8,2]*100

out_pre

#Create Rank-based Mahalanobis Distance Matrix
dist_mat = distmat(t_ind, X_mat)

# Number of matches
	n_matches=1
	
# Moment covariates
mom_covs = cbind(vturn$blackpop_pct1990, vturn$pop90,vturn$college_pct, 
				 vturn$hs_pct, vturn$unemp, vturn$income, vturn$poverty, 
				 vturn$home) 
mom_weights = NULL
mom_tols = c(.0001, 3000, .05, .05, .001, 5000, .05, .02)
 	
# Kolmogorov-Smirnov covariates
ks_covs = cbind(vturn$blackpop_pct1990, vturn$pop90, vturn$unemp) 
ks_n_grid = 15
ks_weights = NULL
ks_tols = c(.00001,3000, .00001)

 
# Covariates for near-exact matching, fine and near-fine balance  
exact_covs = NULL
near_exact_covs = cbind(vturn$year)
near_exact_devs = 2
fine_covs = NULL
near_fine_covs = NULL
near_fine_devs = NULL
 	
# Whether specific controls need to be used
use_controls = NULL
 	
# Enforce all the constraints
enforce_constraints = FALSE
# Whether all the treated units need to be used
	subset_weight = 1
	
out.2 = subsetmatch(dist_mat, t_ind, n_matches, 
					  mom_covs, mom_weights, mom_tols,
					  exact_covs, 
					  near_exact_covs, near_exact_devs, 
					  fine_covs, 
					  near_fine_covs, near_fine_devs, 
					  subset_weight,
					  use_controls,
					  enforce_constraints)
					  
#Compare Number of Treated Obs Before and After Matching
table(t_ind)
length(out.2$t_id)
length(out.2$c_id)

# Create Matched Data
data.match.all <- vturn[c(out.2$t_id, out.2$c_id),]
data.match.all$pair.id <- out.2$group_id
head(data.match.all)
tail(data.match.all)

# Describe covariate balance
KS_out_post <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home, 
							   ks=TRUE, nboots=1000, data=data.match.all)
out_post <- matrix(NA, 8,3)

for (i in 1:8){
	out_post[i,1] <- unlist((KS_out_post$BeforeMatching)[[i]])[3]
  	out_post[i,2] <- unlist((KS_out_post$BeforeMatching)[[i]])[4]
  	out_post[i,3] <- unlist((KS_out_post$BeforeMatching)[[i]])[11]

}

out_post
# Convert Proportions to Percentages
out_post[2,1] <-  out_post[2,1]*100
out_post[3,1] <-  out_post[3,1]*100
out_post[4,1] <-  out_post[4,1]*100
out_post[5,1] <-  out_post[5,1]*100
out_post[7,1] <-  out_post[7,1]*100
out_post[8,1] <-  out_post[8,1]*100

out_post[2,2] <-  out_post[2,2]*100
out_post[3,2] <-  out_post[3,2]*100
out_post[4,2] <-  out_post[4,2]*100
out_post[5,2] <-  out_post[5,2]*100
out_post[7,2] <-  out_post[7,2]*100
out_post[8,2] <-  out_post[8,2]*100

out_post
names <- c("Municipal Population", "African American (%)", "College Degree (%)", 
			"High School (%)", "Unemployed (%)", "Median Income", "Below Pover Line (%)",
			"Home Rule (0/1)")
out <- cbind(out_pre, out_post)			
rownames(out) <- names
out
xtable(out, digits=c(1,1,1,2,1,1,2))

t_id = out.2$t_id	
c_id = out.2$c_id
xtable(finetab(vturn$year, t_id, c_id))


setwd("~/Documents/Black Mayors/Replication File/Matches")
save(data.match.all, file="MatchAll.RData")

#Balance Stats for Data Not In Match
data.notmatch <- vturn[c(-out.2$t_id, -out.2$c_id),]

KS_notmatch <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home, 
							   ks=TRUE, nboots=1000, data=data.notmatch)
out_notmatch <- matrix(NA, 8, 2)

for (i in 1:8){
	out_notmatch[i,1] <- unlist((KS_notmatch $BeforeMatching)[[i]])[3]
  	out_notmatch[i,2] <- unlist((KS_notmatch $BeforeMatching)[[i]])[4]

}

out_notmatch
out_notmatch[2,1] <-  out_notmatch[2,1]*100
out_notmatch[3,1] <-  out_notmatch[3,1]*100
out_notmatch[4,1] <-  out_notmatch[4,1]*100
out_notmatch[5,1] <-  out_notmatch[5,1]*100
out_notmatch[7,1] <-  out_notmatch[7,1]*100
out_notmatch[8,1] <-  out_notmatch[8,1]*100

out_notmatch[2,2] <-  out_notmatch[2,2]*100
out_notmatch[3,2] <-  out_notmatch[3,2]*100
out_notmatch[4,2] <-  out_notmatch[4,2]*100
out_notmatch[5,2] <-  out_notmatch[5,2]*100
out_notmatch[7,2] <-  out_notmatch[7,2]*100
out_notmatch[8,2] <-  out_notmatch[8,2]*100
	
tabout <- cbind(out_notmatch, out_post[,1:2])
rownames(tabout) <- names
tabout
print(xtable(tabout, digits=1), include.rownames=TRUE)

